package com.starblink.brand.ui

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.base.fragment.BaseTVMFragment
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.brand.adapter.FilterInputCell
import com.starblink.brand.bean.FilterPriceInputBean
import com.starblink.brand.databinding.CellFilterPriceBinding
import com.starblink.brand.databinding.FragmentFilterPriceBinding
import com.starblink.brand.model.BrandCollectionVM
import com.starblink.brand.widget.FilterDialogI


/**
 * A simple [Fragment] subclass.
 * Use the [FilterPriceFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class FilterPriceFragment : BaseTVMFragment<FragmentFilterPriceBinding, BrandCollectionVM>(),FilterPriceI {

    companion object {
        fun obtainFragment(lowestPrice:Double?=null, highestPrice:Double?=null): FilterPriceFragment {
            val filterPriceFragment=FilterPriceFragment()
            val bundle=Bundle()
            lowestPrice?.let { bundle.putDouble("lowestPrice",it) }
            highestPrice?.let { bundle.putDouble("highestPrice",it) }
            filterPriceFragment.arguments=bundle
            return filterPriceFragment
        }
    }
    private var filterI: FilterDialogI?=null
    private val priceAdapter by lazyOnNone {
        MutableAdapter(viewModel.filterContentList.value!!).apply {
            addVhDelegate {
                FilterInputCell(CellFilterPriceBinding.inflate(layoutInflater,it,false),this@FilterPriceFragment)
            }
        }
    }
    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentFilterPriceBinding {
        return FragmentFilterPriceBinding.inflate(inflater, container, false)
    }

    override fun startObserve() {
        viewModel.filterContentList.observe(this){
            priceAdapter.notifyDataSetChanged()
        }
    }

    override fun initData() {
        viewBinding.rvFilterPrice.layoutManager = LinearLayoutManager(requireContext())
        viewBinding.rvFilterPrice.adapter = priceAdapter

        val lowestPrice=if(arguments?.containsKey("lowestPrice")==true) arguments?.getDouble("lowestPrice") else null
        val highestPrice=if(arguments?.containsKey("highestPrice")==true) arguments?.getDouble("highestPrice") else null
        viewModel.initFilterPriceDate(lowestPrice,highestPrice)

        this.filterI?.changeRedPointState()
    }

    fun getPriceInput():FilterPriceInputBean{
        return viewModel.filterPriceInputBean
    }

    fun clearPriceInput(){
        viewModel.setFilterPriceDate(null,null)
    }

    override fun changeBtnStates() {
        this.filterI?.changeBtnStates()
    }

    fun setFilterData(filterDialogI: FilterDialogI){
        this.filterI=filterDialogI
    }
}

interface FilterPriceI {
    fun changeBtnStates() //按钮状态修改
}